package com.dsl.dslsuzhou.service;

import com.dsl.dslsuzhou.mapper.DeviceRepository;
import com.dsl.dslsuzhou.model.dto.DeviceQueryRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备业务服务
 */
@Service
@Slf4j
public class DeviceService {

    @Resource
    private DeviceRepository deviceRepository;
    
    /**
     * 根据条件查询设备ID集合
     */
    public List<String> getDeviceIds(DeviceQueryRequest request) {
        // 参数校验
        validateRequest(request);
        
        log.info("查询设备ID: category={}, area={}, building={}, floor={}", 
                request.getCategory(), request.getArea(), request.getBuilding(), request.getFloor());
        
        // 调用Repository查询
        List<String> deviceIds = deviceRepository.findDeviceIdsByConditions(
                request.getCategory(),
                request.getArea(), 
                request.getBuilding(),
                request.getFloor()
        );
        
        log.info("查询到设备ID数量: {}", deviceIds.size());
        
        return deviceIds;
    }
    
    /**
     * 参数校验
     */
    private void validateRequest(DeviceQueryRequest request) {
        if (request == null) {
            throw new IllegalArgumentException("请求参数不能为空");
        }
        
        if (!StringUtils.hasText(request.getCategory())) {
            throw new IllegalArgumentException("设备类别(category)不能为空");
        }
    }
}
